import os
from utility import *


def parse_index_label(df: pd.DataFrame) -> dict:
    output = dict()
    for i, row in df.iterrows():
        index_name = row[INDEX_NAME]
        if not pd.isna(index_name) and isinstance(index_name, str):
            if index_name not in output.keys():
                output[index_name] = {}
                if index_name.startswith('是否'):
                    output[index_name]['字段类别'] = '标签'
                else:
                    output[index_name]['字段类别'] = '指标'
            source = '({}, {}-{})'.format(row[FORM_NAME], row[CATEGORY], row[BUSI_TYPE])
            if '来源报表' not in output[index_name].keys():
                output[index_name]['来源报表'] = {source}
            else:
                output[index_name]['来源报表'].add(source)
            for col in (DETAILED_SUM, INDEX_DIM, UPDATE_PERIOD):
                member = row[col] if not pd.isna(row[col]) else ''
                if col not in output[index_name].keys():
                    output[index_name][col] = {member}
                else:
                    output[index_name][col].add(member)
    return output


def traverse_files(folder_path: str):
    '''
    加载指定文件夹中所有数据表
    :param folder_path: 文件夹路径
    '''
    output = dict()
    for root, dirs, files in os.walk(folder_path):
        for file in files:
            file_path = os.path.join(root, file)
            if file_path.endswith('.xlsx'):
                df = pd.read_excel(io=file_path)
                output = {**output, **parse_index_label(df)}
    output_df_dict = {'字段': [key for key in output.keys()]}
    for col in ('字段类别', '来源报表', DETAILED_SUM, INDEX_DIM, UPDATE_PERIOD):
        output_df_dict[col] = []
        for val in output.values():
            if isinstance(val[col], set):
                output_df_dict[col].append(', '.join(val[col]))
            else:
                output_df_dict[col].append(val[col])
    pd.DataFrame(output_df_dict).to_excel(OUTPUT_FILE_FIELDS_INTERGRATION)


if __name__ == '__main__':
    traverse_files(NOTATED_FOLDER)


